package com.hq.fxh.No2_BBS.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hq.fxh.No1_Infromation.Infor.Activity.News_DetailsActivity;
import com.hq.fxh.No2_BBS.Adapter.Ladapter_KanTieList;
import com.hq.fxh.Modle.MyString;
import com.hq.fxh.Modle.KanTieListModle;
import com.hq.fxh.No6UerSpace.Activity.Space_CompanyActivity;
import com.hq.fxh.R;
import com.hq.fxh.No6UerSpace.Activity.Space_UserActivity;
import com.hq.fxh.Utils.AppConstant;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.SharedPreferenceUtils;
import com.hq.fxh.Utils.Tool;
import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.CustomerView.PopupWindow.PopupWindow_PingBi;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static android.R.attr.value;
import static cn.jpush.android.api.JPushInterface.a.s;
import static cn.jpush.android.api.JPushInterface.a.u;
import static cn.jpush.android.api.JPushInterface.a.v;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :论坛看帖内容页
 * @Created 2017/8/17
 * @Email xlwenhao1858@sina.com
 */


public class BBS_KanTie_ContentFragment extends Fragment {
    protected View rootView;
    protected PullToRefreshListView listViewKanTieContent;
    Ladapter_KanTieList adapter;
    List<KanTieListModle.DataBean> data;
    int pageNum = 1;

    int type; // 1 最新 2 热帖 3 最新回复
    int uid, postid;


    public void fetchData() {
        UserServiceImpl.GetKanTieListUrl(type, pageNum, 20, new Callback<KanTieListModle>() {
            @Override
            public void onResponse(Call<KanTieListModle> call, Response<KanTieListModle> response) {
                listViewKanTieContent.onRefreshComplete();
                KanTieListModle body = response.body();
                List<KanTieListModle.DataBean> data = body.getData();
                if (data != null && data.size() != 0) {
                    if (BBS_KanTie_ContentFragment.this.data == null) {
                        BBS_KanTie_ContentFragment.this.data = new ArrayList<KanTieListModle.DataBean>();
                    } else {
                        if (pageNum == 1) {
                            BBS_KanTie_ContentFragment.this.data.clear();
                        }
                    }
                    BBS_KanTie_ContentFragment.this.data.addAll(data);
                    adapter.setData(BBS_KanTie_ContentFragment.this.data);
                    adapter.notifyDataSetChanged();

                } else {
                    LogUtils.startToash(getContext(), MyString.nullDataString);
                }

            }

            @Override
            public void onFailure(Call<KanTieListModle> call, Throwable t) {
                listViewKanTieContent.onRefreshComplete();

            }
        });

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_kantie_content, null);
        this.type = getArguments().getInt("type");
        initView(rootView);
        initData();
        fetchData();
        return rootView;
    }

    private void initData() {
        uid = SharedPreferenceUtils.getMemberId(getContext());

        listViewKanTieContent.setMode(PullToRefreshBase.Mode.BOTH);
        listViewKanTieContent.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageNum = 1;
                fetchData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageNum = pageNum + 1;
                fetchData();

            }
        });

        adapter = new Ladapter_KanTieList(getContext(), new Ladapter_KanTieList.KanTieCallBack() {
            @Override
            public void onCall(int position, LinearLayout layout) {
                showPopup(layout, position);
            }

            @Override
            public void contentCall(int position, Object odata) {

                KanTieListModle.DataBean dataBean = data.get(position);
                int authorID = dataBean.getMember_id();
                if (authorID == 0) {
                    LogUtils.startToash(getContext(), AppConstant.userNo);
                    return;
                }
                Intent intent = new Intent(getContext(), Space_UserActivity.class);
                intent.putExtra("uid", authorID);
                LogUtils.e(position + "   选择的特满足    " + authorID);
                startActivity(intent);

            }
        });
        listViewKanTieContent.setAdapter(adapter);


        listViewKanTieContent.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                LogUtils.e("item==" + i);
                if (i > 0) {
                    i = i - 1;
                }
                Intent intent = new Intent(getContext(), News_DetailsActivity.class);
                KanTieListModle.DataBean dataBean = null;
                if (data != null && data.size() > i) {
                    dataBean = data.get(i);
                    postid = dataBean.getPost_id();
                    String value = Tool.getplateUrl(uid, postid, 2);
                    LogUtils.e("url==" + value);
                    intent.putExtra("url", value);
                    intent.putExtra("type", AppConstant.Type_Plate);
                    intent.putExtra("data", dataBean);
                }
                intent.putExtra("url", Tool.getplateUrl(uid, postid, 2));


                startActivity(intent);
            }
        });
    }

    PopupWindow_PingBi popupWindow_pingBi;

    private void showPopup(View view, final int parentsPosition) {
        View popview = View.inflate(getActivity(), R.layout.popup_bbs_pingbi, null);

        ListView list = (ListView) popview.findViewById(R.id.list_PopupBBS);

        List<String> objects = new ArrayList<>();

        objects.add("关注此人");
        objects.add("举报");
        objects.add("加入黑名单");
        objects.add("删除");
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                clickPopup(parentsPosition, i);

            }
        });

        ArrayAdapter<String> popadapter = new ArrayAdapter<String>(getContext(), R.layout.pop_text_item, R.id.popup_Text_item, objects);
        list.setAdapter(popadapter);
        Tool.setListViewHeightBasedOnChildren(list);
        popupWindow_pingBi = new PopupWindow_PingBi();
        popupWindow_pingBi.showPopupWindow(getActivity(), popview, view);

    }

    private void clickPopup(int parentsPosition, int i) {
        String id = null;
        try {
            KanTieListModle.DataBean dataBean = data.get(parentsPosition);
            id = "" + dataBean.getMember_id();
        } catch (Exception e) {
            LogUtils.e("BBS_KanTie_Content   " + e.getMessage());
        }
        popupWindow_pingBi.dismissPopup();
        switch (i) {
            case 0:
                addFollowUser(id);
                break;
            case 1:

                break;
            case 2:
                addBlackUser(id);
                break;
            case 3:

                break;
        }
        /*
        * 处理看帖中的弹窗点击事件
        * */

    }

    private void initView(View rootView) {
        listViewKanTieContent = (PullToRefreshListView) rootView.findViewById(R.id.listView_KanTie_Content);
    }

    public static BBS_KanTie_ContentFragment newInstance(int type) {

        Bundle args = new Bundle();
        args.putInt("type", type);
        BBS_KanTie_ContentFragment fragment = new BBS_KanTie_ContentFragment();
        fragment.setArguments(args);
        return fragment;
    }


    public void addFollowUser(String to_uid) {

        UserServiceImpl.addFollowerUser(getContext(), 1, "" + to_uid, new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    ResponseBody body = response.body();
                    if (body != null) {
                        LogUtils.startToash(getContext(), body.string());

                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    LogUtils.startToash(getContext(), "失败");

                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

                LogUtils.startToash(getContext(), "失败");


            }
        });
    }

    public void addBlackUser(String to_uid) {
        LogUtils.e("to_uid  " + to_uid);
        UserServiceImpl.addBlackUser(getContext(), 1, to_uid, new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    LogUtils.startToash(getContext(), response.body().string());
                } catch (IOException e) {
                    e.printStackTrace();
                    LogUtils.startToash(getContext(), "失败");

                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

                LogUtils.startToash(getContext(), "失败");

            }
        });
    }

}
